package bz.yfw.example.s3blazeds.part2.datastore;

import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Transaction;

import bz.yfw.example.s3blazeds.part2.meta.entity.EmpEntityModelMeta;
import bz.yfw.example.s3blazeds.part2.model.entity.EmpEntityModel;

public class EmpEntityDataStore {
    
    private static final EmpEntityModelMeta m = EmpEntityModelMeta.get();
    
    public static void entry(EmpEntityModel entity) throws Throwable {
        
        final Transaction tx = Datastore.beginTransaction();
        try {
            EmpEntityModel result = 
                Datastore.query(m).filter(m.name.equal(entity.getName())).asSingle();
            
            if (result == null) {
                Datastore.put(tx, entity);
            } else {
                String km = result.getKey().getKind();
                String name = result.getName();
                String sex = result.getEntryTime() + "";
                System.out.println(km + " " + name + " " + sex);
            }
            tx.commit();
        } catch (Throwable e){
            e.printStackTrace();
            throw e;
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }
    }
}